﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ExamCommon
{
    /// <summary>
    /// 消息类, 用于网络传输, 声明可序列化
    /// </summary>
    [Serializable]
    public class Message
    {
        /// <summary>
        /// 消息来源,ip地址和port端口号
        /// </summary>
        public EndPoint fromEndPoint { get; set; } = null;

        /// <summary>
        /// 消息目的地,ip地址和port端口号
        /// </summary>
        public EndPoint toEndPoint { get; set; } = null;

        /// <summary>
        /// 消息命令类型
        /// </summary>
        public MsgCommand msgCommand { get; set; } = MsgCommand.NONE;

        /// <summary>
        /// 消息类型
        /// </summary>
        public MsgType msgType { get; set; } = MsgType.NONE;

        /// <summary>
        /// 消息发送状态
        /// </summary>
        public MsgSendState sendState { get; set; } = MsgSendState.SINGLE;

        /// <summary>
        /// Object对象消息内容
        /// </summary>
        public object objectData { get; set; } = null;

        /// <summary>
        /// byte字节消息内容
        /// </summary>
        public byte[] byteData { get; set; } = null;

        /// <summary>
        /// 根据传入的socket初始化消息来源和消息目的地EndPoint
        /// </summary>
        /// <param name="socket"></param>
        public void initEndPoint(Socket socket)
        {
            this.fromEndPoint = socket.LocalEndPoint;
            this.toEndPoint = socket.RemoteEndPoint;
        }
    }
}
